package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.ui.UIHelper;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 17/2/14 16:22
 * 邮箱：hqy_xz@126.com
 */

public class ZoomDialogFragment extends DialogFragment {

    @Bind(R.id.iv_zoom)
    ImageView mIvZoom;

    private String url;


    public static ZoomDialogFragment newInstance(String imgUrl) {
        ZoomDialogFragment zoom = new ZoomDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(UIHelper.URL, imgUrl);
        zoom.setArguments(bundle);
        return zoom;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {
            dismiss();
            return;
        }
        url = getArguments().getString(UIHelper.URL);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Share);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_zoom, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        init();
        return builder.create();
    }


    private void init() {
        Glide.with(getActivity())
                .load(url)
                .into(mIvZoom);
    }

    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.CENTER;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick(R.id.iv_zoom)
    public void onClick() {
        dismiss();
    }
}
